package pc.util;

import java.util.*;

public class DateUtils
{
  private static Calendar calendar = Calendar.getInstance();

  public static java.sql.Date toDate(int year,int month,int day)
  {
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(year,month-1,day);
    return new java.sql.Date(cal.getTime().getTime());
  }

  public static int getYear(java.util.Date date)
  {
    if (date == null) date = new java.util.Date(0);
    calendar.setTime(date);
    return calendar.get(Calendar.YEAR);
  }

  public static int getMonth(java.util.Date date)
  {
    if (date == null) date = new java.util.Date(0);
    calendar.setTime(date);
    return calendar.get(Calendar.MONTH) + 1;
  }

  public static int getDay(java.util.Date date)
  {
    if (date == null) date = new java.util.Date(0);
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_MONTH);
  }


  public static int getDayOfWeek(java.util.Date date)
  {
    if (date == null) date = new java.util.Date(0);
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_WEEK);
  }

  public static java.sql.Date skipDate(java.util.Date date,int num_day)
  {
    if (date == null) date = new java.util.Date(0);
    calendar.setTime(date);
    calendar.add(Calendar.DATE,num_day);
    return new java.sql.Date(calendar.getTime().getTime());
  }

  public static java.sql.Date nextDate(java.util.Date date)
  {
    return skipDate(date,1);
  }

  public static java.sql.Date previousDate(java.util.Date date)
  {
    return skipDate(date,-1);
  }

  public static java.sql.Date toTime(int hour,int min,int sec)
  {
    long ms = (hour*60*60 + min*60 + sec)*100;
    return new java.sql.Date(ms);
  }

  public static java.sql.Date skipTime(java.util.Date date,int sec)
  {
    if (date == null) date = new java.util.Date(0);
    long ms = date.getTime() + (sec*1000);
    return new java.sql.Date(ms);
  }

  public static boolean between(java.util.Date date,java.util.Date from_date,java.util.Date to_date)
  {
    return (date.compareTo(from_date) >=0) && (date.compareTo(to_date) <= 0);
  }

  public static java.sql.Date getMonthLater(java.util.Date date)
  {
    java.util.Date to_date = date;
    int to_day = DateUtils.getDay(DateUtils.previousDate(date));
    while (DateUtils.getDay(to_date) != to_day) {
      to_date = DateUtils.nextDate(to_date);
    }
    return new java.sql.Date(to_date.getTime());
  }


}
